package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import model.College;
import model.Teacher;
import model.User;
import model.Users;
import dataLayer.DAOUser;
import threads.LoadingDataAbsences;
import threads.LoadingDataCollege;
import threads.LoadingDataUsers;
import types.ConsoleType;
import view.Main_Frame;
import view.PanelDASboard;
import view.PanelHolder;
import view.PanelTeacherList;
import view.loginPanel;

public class Login implements ActionListener, KeyListener {
	private String username;
	private String password;
	private String passwordField;
	private loginPanel login;
	int trycount = 0;
	String admin = "L. Kadri";

	// final JFrame frame = new JFrame("JProgress Demo");

	public Login(loginPanel login) {
		this.login = login;

	}

	// Check on fields for login and message in console
	public void actionPerformed(ActionEvent e) {
		if ("Inloggen".equals(e.getActionCommand())) {
			String message = null;
			ConsoleType type = null;

			username = login.getUsernameField().getText();
			passwordField = login.getPasswordField().getText();

			if (username.equals(admin)) {

				password = "adminlyla";

			} else {

				User u = new User(username, null);
				password = DAOUser.getInstance().loadUser(u);
			}

			if (passwordField.equals(password)) {
				Console c = Console.getInstance();
				message = "Laden gegevens... ";
				type = ConsoleType.REFRESH;
				Console.getInstance().setConsole(message, type);

				c.getJConsole().update(c.getGraphics());

				College.getInstance();

				Users.getInstance();

				Teacher.getInstance();

				message = "Welkom " + username;
				type = ConsoleType.SUCCESS;
				Main_Frame mf = Main_Frame.getInstance();
				PanelHolder hold = PanelHolder.getInstance();

				mf.remove(loginPanel.getInstance());

				hold.add(PanelDASboard.getInstance());
				mf.add(hold);
				mf.repaint();
				mf.validate();
				hold.repaint();
				hold.validate();

			} else {
				if (trycount == 0) {
					message = "Gegevens onjuist, probeer opnieuw ";
					type = ConsoleType.ERROR;
					trycount = 1;
				} else if (trycount <= 1) {
					message = "Gegevens onjuist, blijft dit probleem zich voordoen neem contact op met L. Kadri ";
					type = ConsoleType.ERROR;
					trycount = 2;
				}
			}
			Console.getInstance().setConsole(message, type);

		}

	}

	@Override
	public void keyPressed(KeyEvent evt) {
		if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

			String message = null;
			ConsoleType type = null;

			username = login.getUsernameField().getText();
			passwordField = login.getPasswordField().getText();

			if (username.equals(admin)) {

				password = "adminlyla";

			} else {

				User u = new User(username, null);
				password = DAOUser.getInstance().loadUser(u);
			}

			if (passwordField.equals(password)) {
				Console c = Console.getInstance();
				message = "Laden gegevens... ";
				type = ConsoleType.REFRESH;
				Console.getInstance().setConsole(message, type);

				c.getJConsole().update(c.getGraphics());

				College.getInstance();

				Users.getInstance();

				Teacher.getInstance();

				message = "Welkom " + username;
				type = ConsoleType.SUCCESS;
				Main_Frame mf = Main_Frame.getInstance();
				PanelHolder hold = PanelHolder.getInstance();

				mf.remove(loginPanel.getInstance());

				hold.add(PanelDASboard.getInstance());
				mf.add(hold);
				mf.repaint();
				mf.validate();
				hold.repaint();
				hold.validate();

			} else {
				if (trycount == 0) {
					message = "Gegevens onjuist, probeer opnieuw ";
					type = ConsoleType.ERROR;
					trycount = 1;
				} else if (trycount <= 1) {
					message = "Gegevens onjuist, blijft dit probleem zich voordoen neem contact op met L. Kadri ";
					type = ConsoleType.ERROR;
					trycount = 2;
				}
			}
			Console.getInstance().setConsole(message, type);
			
		}

	}

	public void keyReleased(KeyEvent arg0) {

	}

	public void keyTyped(KeyEvent arg0) {

	}

}
